unit Unit16; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Buttons, Unit1, sqldb, db, DbCtrls;

type

  { TForm16 }

  TForm16 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Datasource1: TDatasource;
    DBComboBox1: TDBComboBox;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Label1: TLabel;
    Label10: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    SQLQuery1: TSQLQuery;
    SQLQuery2: TSQLQuery;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow;
  private
    { private declarations }
  public
    codigo : string;
    iva : string;
  end; 

var
  Form16: TForm16; 

implementation

{ TForm16 }

procedure TForm16.FormShow;
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
    
  if SQLQuery2.Active = True then
    SQLQuery1.Close;
    
  SQLQuery1.DataBase := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;
  
  SQLQuery2.DataBase := Form1.PQConnection1;
  SQLQuery2.Transaction := Form1.SQLTransaction1;
  
  SQLQuery1.SQL.Text := 'select riva_descripcion from responsabilidad_iva order by riva_descripcion';

  SQLQuery1.Open;

  DBComboBox1.DataField:= 'riva_descripcion';
  DBComboBox1.Clear;
  
  while not SQLQuery1.EOF do
  begin
    DBComboBox1.Items.Add(SQLQuery1.Fields[0].AsString);
    SQLQuery1.Next;
  end;
  
  Edit1.Text := '';
  Edit2.Text := '00000000000';
  DBComboBox1.Text := '';
  Edit3.Text := '';
  Edit4.Text := '';
  Edit5.Text := '';
  Edit6.Text := '';
  
  iva := '';
  codigo := '';
  
  Edit1.SetFocus;
end;

procedure TForm16.Button2Click(Sender: TObject);
begin
  Close;
end;

procedure TForm16.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
    
  if SQLQuery2.Active = True then
    SQLQuery1.Close;
end;

procedure TForm16.Button1Click(Sender: TObject);
var
  QueryString : String;
  
begin
  if Edit1.Text <> '' then
  begin
    if Edit2.Text = '' then
      Edit2.Text := '00000000000';

    QueryString := 'insert into cliente(cln_nombre, cln_cuit, cln_iva, cln_direccion, cln_email, '
      + 'cln_telefono, cln_region) values(''' + Edit1.Text + ''', ''' + Edit2.Text
      +  ''', (select riva_cdg ' + 'from responsabilidad_iva where riva_descripcion = '''
      + DBComboBox1.Text + '''), ''' + Edit3.Text + ''', ''' + Edit4.Text + ''', ''' + Edit5.Text
      + ''', ''' + Edit6.Text + ''')';
    
    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');

    ShowMessage('ALTA EXITOSA');
    
    SQLQuery2.SQL.Text := 'select max(cln_cdg) from cliente where cln_nombre = '''
    + Edit1.Text + '''';
    
    SQLQuery2.Open;
    
    while not SQLQuery2.EOF do
    begin
      codigo := SQLQuery2.Fields[0].AsString;
      SQLQuery2.Next;
    end;
    
    SQLQuery2.Close;
    
    SQLQuery2.SQL.Text := 'select cln_iva from cliente where cln_cdg = ' + codigo;

    SQLQuery2.Open;

    while not SQLQuery2.EOF do
    begin
      iva := SQLQuery2.Fields[0].AsString;
      SQLQuery2.Next;
    end;

    SQLQuery2.Close;
    
    Close;
  end
  else
    ShowMessage('ERROR EN EL INGRESO DE DATOS');
end;

initialization
  {$I unit16.lrs}

end.

